var revenueApi = require('../../script/revenue.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CONS_NO: '',
    activeIndex: '1',
    HeatYearList: [], //年费
    PayList: [], //收费
    AuditList: [], //稽查
    OrderList: [], //工单
    HistroyList: [], //变更
    CurrentPage: 1,
    PageSize: 20,
    HasNextPage: true,
    noData: false,
    isReachBottom: false,
  },
  _refreshData() {
    this.setData({
      CurrentPage: 1,
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({
        CONS_NO: options.id
      });
    }
    this.getHeatYearList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.activeIndex === '1' || this.data.activeIndex === '4') return;
    this.setData({
      isReachBottom: true
    });
    if (this.data.HasNextPage) {
      this.setData({
        CurrentPage: this.data.CurrentPage + 1
      })
      if (activeIndex === '2') {
        this.getPayList();
      }
      if (activeIndex === '3') {
        this.getAuditList();
      }
      if (activeIndex === '4') {
        this.getOrderList();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tabChange(e) {
    const activeIndex = e.detail.name
    this.setData({
      activeIndex
    })
    if (activeIndex === '1' && this.data.HeatYearList.length === 0) {
      this.getHeatYearList();
    }
    if (activeIndex === '2' && this.data.PayList.length === 0) {
      this._refreshData();
      this.getPayList(true);
    }
    if (activeIndex === '3' && this.data.AuditList.length === 0) {
      this._refreshData();
      this.getAuditList(true);
    }
    if (activeIndex === '4' && this.data.OrderList.length === 0) {
      this._refreshData();
      this.getOrderList(true);
    }
    if (activeIndex === '5' && this.data.HistroyList.length === 0) {
      this.getHistroyList();
    }
  },
  getHeatYearList() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      model: {
        CONS_NO: this.data.CONS_NO
      }
    }
    revenueApi.HeatYearListPost(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          HeatYearList: res.data || []
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  getPayList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      parameter: {
        CONS_NO: this.data.CONS_NO,
        pagination: {
          rows: this.data.PageSize,
          page: 1,
          sidx: "charge_date",
          sord: "desc",
        }
      }
    }
    revenueApi.GetPayDetailInfo(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.DataList]
        } else {
          dataList = [...this.data.PayList, ...res.data.DataList]
        }
        let HasNextPage = res.data.HasNextPage;
        const noData = dataList.length === 0;
        this.setData({
          PayList: res.data,
          HasNextPage,
          noData
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  getAuditList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      parameter: {
        CONS_NO: this.data.CONS_NO,
        pagination: {
          rows: this.data.PageSize,
          page: 1,
          sidx: "",
          sord: "desc",
        },
        PLAN_STATUS: 0,
        STATUS: 99,
      }
    }
    revenueApi.GetAllDetailPageList(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        const resData = res.data.Items || [];
        let dataList = []
        if (isRefresh) {
          dataList = [...resData]
        } else {
          dataList = [...this.data.AuditList, ...resData]
        }
        let HasNextPage = res.data.HasNextPage;
        const noData = dataList.length === 0;
        this.setData({
          AuditList: res.data,
          HasNextPage,
          noData
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  getOrderList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      model: {
        queryParam: {
          ConsNo: this.data.CONS_NO,
        },
        pagination: {
          rows: this.data.PageSize,
          page: 1,
          sidx: "",
          sord: "desc",
        },
      }
    }
    revenueApi.InvokeGetList(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        const resData = res.data.Items || [];
        let dataList = []
        if (isRefresh) {
          dataList = [...resData]
        } else {
          dataList = [...this.data.OrderList, ...resData]
        }
        let HasNextPage = res.data.HasNextPage;
        const noData = dataList.length === 0;
        this.setData({
          OrderList: res.data,
          HasNextPage,
          noData
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  getHistroyList() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      model: {
        CONS_NO: this.data.CONS_NO,
        STATUS_CODE: 0
      }
    }
    revenueApi.GetWorkFlowHistoryForJC(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          HistroyList: res.data ||[]
        });
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
})